//
//  SinaWelcomeSnapKitViewController.swift
//  weibo
//
//  Created by liuchao on 16/1/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit


class SinaWelcomeSnapKitViewController: UIViewController {

    var bottomSnapKitConstraint:Constraint?
    
    
    override func loadView() {
        super.loadView()
        view = UIImageView(image: UIImage(named: "ad_background"))
        
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

       setupUI()
    }
     // view 已经显示出来
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 更新约束

        bottomSnapKitConstraint?.updateOffset(-450)
    
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            // 强制 快速 刷新 产生 动画
            self.view.layoutIfNeeded()
            }) { (_ ) -> Void in
                
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificationEnter, object: nil)
        }
        
    }
    //
    private func setupUI(){
        
        view.addSubview(photoImageView)
        view.addSubview(nameLabel)
    
        
        //// 头像的约束 -- 水平居中,距离底部有一个距离 150
//        photoImageView.snp_makeConstraints { (make) -> Void in
//            
//            // make相当于 photoImageView
//            make.centerX.equalTo(view.snp_centerX)
//            // 一定要关联我的约束
//            bottomSnapKitConstraint = make.bottom.equalTo(view.snp_bottom).offset(-150).constraint
//        }
//        
//        //欢迎文字设置约束 -- 水平居中, 顶部等于photoImageView底部+ 一个距离
//        nameLabel.snp_makeConstraints { (make) -> Void in
//            
//            make.centerX.equalTo(view.snp_centerX)
//            make.top.equalTo(photoImageView.snp_bottom).offset(10)
//            
//        
//            
//        }
        
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            
            bottomSnapKitConstraint = make.bottom.equalTo(view.snp_bottom).offset(-150).constraint
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
        }
        
    }
    
    
    //MARK:--懒加载
    
    // 头像
    lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        // ???? 是除以 4 还是除以 2
        img.layer.cornerRadius = 170 / 2.0 / 2.0
        
        img.layer.masksToBounds = true
        // 边框的宽度
        img.layer.borderWidth = 3
        // 边框的颜色
        img.layer.borderColor = UIColor.yellowColor().CGColor
        
        // 设置大小
        
        return img
        
        
    }()
    
    //欢迎文字
    lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        
        // 属性
        label.text = "欢迎归来"
        
        //字体和颜色
        label.font = UIFont.systemFontOfSize(16)
        //
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()

}
